// 数据的处理 -- 操作localstorage

import DataHelper from "./DataHelper";
import ItemData from "@/mode/ItemData";
import Category from "@/mode/CateEnum";

class ActionHelper {
  dataHepler: DataHelper = new DataHelper('memoData', 'id')
  memoList: Array<ItemData>
  constructor() {
    this.memoList = this.readData()
  }

  readData():Array<ItemData> {
    let arrObj = this.dataHepler.readData()
    let arrItem = arrObj.map((ele:any) => {
      let item:ItemData = new ItemData()
      item.id = ele.id;
      item.cateGoryId = ele.cateGoryId;
      item.title = ele.title;
      item.content = ele.content;
      item.createTime = ele.createTime;
      return item
    })
    return arrItem
  }
  add(item: ItemData):number {
    item.id=this.dataHepler.addData(item)
    this.memoList.push(item)
    return item.id
  }
  getCategoryName(cateId: Category):string {
    const arrNames = ['工作', '生活', '学习']
    return arrNames[cateId]
  }
  edit(item: ItemData):void {
    let editItem: ItemData | undefined = this.memoList.find(ele => {
      return ele.id == item.id
    })
    if(editItem) {
      editItem.cateGoryId = item.cateGoryId
      editItem.title = item.title
      editItem.content = item.content

      this.dataHepler.saveData(this.memoList)
    }
  }

  remove(id:number):void {
    let index:number = this.memoList.findIndex(ele => {
      return ele.id == id
    })
    if(index>-1) {
      this.memoList.splice(index, 1)
      this.dataHepler.saveData(this.memoList)
    }
  }
}

export default ActionHelper